package ade.prime.activity;

import android.graphics.Shader.TileMode;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;

/**
 * Vue des pages d'aide de l'application.
 * Navigable grâce au "swip", mais aussi aux cliques sur les liens du sommaire. Retour au sommaire en cliquant sur le bouton "sommaire de la barre d'action.
 * @author Seb
 *
 */
public class UserManual extends SherlockFragmentActivity
implements BundleExtraNames {
	
	protected static final int DEFAULT_PAGE_NUMBER = 1 ;
	
    static final int NUM_ITEMS = 5;
    
    protected static final int SUMMARY_ID = 0 ;

    protected MyAdapter mAdapter;

    protected ViewPager mPager;
    
    protected static final int SUMMARY = 0;
    protected static final int TITLE_NUMETUD = 1;
    protected static final int TITLE_RESOURCES = 2;
    protected static final int TITLE_RENS_RESOURCES = 3;
    protected static final int TITLE_LEGEND = 4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {   	
    	//Ajoute une petite flèche à gauche du logo dans la barre d'action
    	getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    	//Ajoute un sous-titre à la barre d'action
    	getSupportActionBar().setSubtitle(R.string.UserManual_subtitle);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user_manual_pager);
        
      //This is a workaround for http://b.android.com/15340 from http://stackoverflow.com/a/5852198/132047
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            BitmapDrawable bg = (BitmapDrawable)getResources().getDrawable(R.drawable.bg_striped);
            bg.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
            getSupportActionBar().setBackgroundDrawable(bg);

            BitmapDrawable bgSplit = (BitmapDrawable)getResources().getDrawable(R.drawable.bg_striped_split_img);
            bgSplit.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
            getSupportActionBar().setSplitBackgroundDrawable(bgSplit);
        }

        this.mAdapter = new MyAdapter(getSupportFragmentManager());

        this.mPager = (ViewPager)findViewById(R.id.user_manual_viewpager);
        this.mPager.setAdapter(this.mAdapter);                   
    }
    
    /**
     * Affiche les boutons/options de la barre d'action.
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(Menu.NONE, SUMMARY_ID, Menu.NONE, R.string.UserManual_sommaire)
            .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
        return super.onCreateOptionsMenu(menu);
    }
    
    /**
     * Gére les actions des boutons/options de la barre d'action.
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	switch(item.getItemId()) {
    	case android.R.id.home:
    		if (this.mPager.getCurrentItem() == 0) {
    			this.onBackPressed() ;
    			break ;
    		}// if
			//$FALL-THROUGH$
		case SUMMARY_ID:
            //Retourne au sommaire
    		this.mPager.setCurrentItem(0) ;
    	}// switch
        return true;
    }

	/**
	 * Adapteur pour les fragments.
	 * @author Seb
	 *
	 */
	public static class MyAdapter extends FragmentPagerAdapter {
        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public int getCount() {
            return NUM_ITEMS;
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public Fragment getItem(int position) {
            return HelpFragment.newInstance(position);
        }
        
    }

    /**
     * Gére l'affichage d'un fragment.
     * Une fragment étant ici, une page d'aide.
     * @author Seb
     *
     */
    public static class HelpFragment extends SherlockFragment {
        int mNum;
        ViewPager mPager;

        /**
         * Créer une nouvelle instance de HelpFragment, contenant "num" (=id du titre de l'aide)
         * comme argument.
         */
        static HelpFragment newInstance(int num) {
            HelpFragment f = new HelpFragment();
            // Supply num input as an argument.
            Bundle args = new Bundle() ;
            args.putInt(EXTRA_USER_MANUAL_PAGE_NUMBER, num) ;
            f.setArguments(args);

            return f;
        }

        /**
         * A la création, retrouve "num" argument de cette instance.
         */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState) ;
            Bundle args = this.getArguments() ;
            if (args != null) {
            	this.mNum = args.getInt(EXTRA_USER_MANUAL_PAGE_NUMBER, 
            			DEFAULT_PAGE_NUMBER) ;
            }// if
        }

        /**
         * Ce fragment appelle la page d'aide associée au titre de l'aide, grâce à "num".
         */
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View v;
            int[] tabIdTitle = new int[] {R.id.help_title_summary, R.id.help_title_1, R.id.help_title_2, R.id.help_title_3, R.id.help_title_4};
            switch (this.mNum) {
        	case SUMMARY :
        		v = inflater.inflate(R.layout.user_manual_summary, container, false);
        		this.mPager = (ViewPager)container.findViewById(R.id.user_manual_viewpager);
                //Action des liens
        		for (int ii = 0; ii < NUM_ITEMS; ii++) {
        			final int i = ii;
                	TextView link = (TextView)v.findViewById(tabIdTitle[i]);
                    link.setOnClickListener(new OnClickListener() {
                        @Override
						public void onClick(View view) {
                            HelpFragment.this.mPager.setCurrentItem(i);
                        }
                    }); 
        		}
       		 break;
        	case TITLE_NUMETUD :
                v = inflater.inflate(R.layout.user_manual_title_numetud, container, false);
       		 break;
        	case TITLE_RESOURCES :
        		v = inflater.inflate(R.layout.user_manual_title_numresources, container, false);
       		 break;
        	case TITLE_RENS_RESOURCES :
        		v = inflater.inflate(R.layout.user_manual_title_rensresources, container, false);
       		 break;
        	case TITLE_LEGEND :
        		v = inflater.inflate(R.layout.user_manual_title_legend, container, false);
    		 break;
    		default :
        		v = inflater.inflate(R.layout.user_manual_summary, container, false);
        		this.mPager = (ViewPager)container.findViewById(R.id.user_manual_viewpager);
                //Action des liens
        		for (int ii = 0; ii < NUM_ITEMS; ii++) {
        			final int i = ii;
                	TextView link = (TextView)v.findViewById(tabIdTitle[i]);
                    link.setOnClickListener(new OnClickListener() {
                        @Override
						public void onClick(View view) {
                            HelpFragment.this.mPager.setCurrentItem(i) ;
                        }
                    }); 
        		}
    	}
            return v;
        }

    }   
}
